package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootBfinfo;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.JsonReturn;
import org.footballmanager.pandafootball.system.domain.enums.BFLX;
import org.footballmanager.pandafootball.system.domain.enums.BFZT;
import org.footballmanager.pandafootball.system.form.impl.FootBfinfoSearchForm;
import org.footballmanager.pandafootball.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
//@SessionAttributes("footBfinfoSearchForm")
@RequestMapping("/hy/footBfinfo")
public class HyBfinfoController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footBfinfo";

    @Autowired
    private FootBfinfoService footBfinfoService;
    @Autowired
    private FootUserPersonService footUserPersonService;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootSjService footSjService;
    @Autowired
    private FootBfDetailService footBfDetailService;
    @Autowired
    private FootBfMvpService footBfMvpService;

    /**
     * 初始化
     *
     * @param map                  参数
     * @param footBfinfoSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        map.addAttribute("sjList", footSjService.getall());
        FootBfinfoSearchForm newFootBfinfoSearchForm = new FootBfinfoSearchForm();
        if (footBfinfoSearchForm != null) {
            newFootBfinfoSearchForm = footBfinfoSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootBfinfoSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, Integer type, HttpServletRequest request,
                         @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        footBfinfoSearchForm.setBothQd(userId);
        footBfinfoSearchForm.setBflx(BFLX.values()[type]);
        if (!type.equals(0)) {
            if (footBfinfoSearchForm.getSj() == null) {
                footBfinfoSearchForm.setSj(footSjService.getSj());
            }
        } else {
            footBfinfoSearchForm.setSj(null);
        }
        footBfinfoService.search(ENTITYNAME, map, footBfinfoSearchForm);

        map.addAttribute(String.format(Constants.LISTENTITY, "lastFootBfinfoList"), footBfinfoService.findLastSjData(userId));
        return footBfinfoService.searchPage(footBfinfoSearchForm, true);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map, HttpServletRequest request, @RequestParam Integer type) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        return footBfinfoService.add(user, map, type, request);
    }

    @RequestMapping(value = "/searchUser", method = RequestMethod.POST)
    @ResponseBody
    public List<FootUser> searchUser(HttpServletRequest request) {
        return footUserService.getAllWithoutAdmin(request);
    }

    @RequestMapping(value = "/searchPerson", method = RequestMethod.POST)
    @ResponseBody
    public List<FootPerson> searchPerson(@RequestParam Long userId, HttpServletRequest request) {
        return footUserPersonService.findFootPersonByUserId(userId, request);
    }

    @RequestMapping(value = "/saveTemp", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn saveTemp(@RequestParam("team") Long team, @RequestParam("type") Integer type,
                               @RequestParam("ids") String ids, @RequestParam("nums") String nums,
                               HttpServletRequest request) {
        footBfDetailService.saveEntityTemp(team, type, ids, nums, request);
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/saveTempMvp", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn saveTempMvp(@RequestParam("team") Long team, @RequestParam("id") Long id,
                                  HttpServletRequest request) {
        footBfMvpService.saveEntityTemp(team, id, request);
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute FootBfinfo footBfinfo,
                       BindingResult bindingResult, HttpServletRequest request, Model map, SessionStatus s) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        footBfinfoService.processError(footBfinfo, bindingResult, request);
        if (!bindingResult.hasErrors()) {
            footBfinfoService.save(footBfinfo, request);
            footBfDetailService.saveEntity(footBfinfo, request);
            footBfMvpService.saveEntity(footBfinfo, request);
            s.setComplete();
            return "redirect:/hy/footBfinfo/view?id=" + footBfinfo.getId();
        } else {
            return footBfinfoService.processReturnPage(footBfinfo, map, user, request);
        }
    }

    @RequestMapping(value = "/mySearch")
    public String mySearch(Model map, Integer type, HttpServletRequest req,
                           @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        FootUser user = (FootUser) req.getSession().getAttribute(Constants.USER);
        footBfinfoSearchForm.setBflx(BFLX.values()[type]);
        if (!type.equals(0)) {
            footBfinfoSearchForm.setSj(footSjService.getSj());
        } else {
            footBfinfoSearchForm.setSj(null);
        }
        footBfinfoSearchForm.setBfqd(user.getId());
        footBfinfoSearchForm.setBfzt(BFZT.NOT);
        footBfinfoService.search(ENTITYNAME, map, footBfinfoSearchForm);
        return footBfinfoService.searchPage(footBfinfoSearchForm, false);
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                               @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        return footBfinfoService.view(id, map, request, footBfinfoSearchForm, true);
    }

    @RequestMapping(value = "/myView", method = RequestMethod.GET)
    public String openMyViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                                 @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        return footBfinfoService.view(id, map, request, footBfinfoSearchForm, false);
    }

    @RequestMapping(value = "/makesure", method = RequestMethod.POST)
    public String makesure(@ModelAttribute FootBfinfo footBfinfo, HttpServletRequest request, Model map,
                           SessionStatus s, @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        footBfinfoSearchForm.setBfzt(null);
        footBfinfoSearchForm.setSj(null);
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footBfinfoSearchForm);
        footBfinfoService.makesure(footBfinfo, request);
        s.setComplete();
        return "redirect:/hy/footBfinfo/view?id=" + footBfinfo.getId();
    }

    @RequestMapping(value = "/makesure", method = RequestMethod.GET)
    public String makesure(@RequestParam Long id, HttpServletRequest request,
                           SessionStatus s, @ModelAttribute FootBfinfoSearchForm footBfinfoSearchForm) {
        FootBfinfo bf = footBfinfoService.get(id);
        bf.setBfzt(BFZT.HAVE);
        footBfinfoService.updateAll(bf, request);
        s.setComplete();
        return "redirect:/hy/footBfinfo/mySearch?type=0";
    }
}
